#! /usr/bin/python

import signal, sys, time, os, struct

UPDATE_OFFSET = 0
IR_OFFSET = 5
CO2_OFFSET = 12
SONAR_OFFSET = 19



def signal_handler(signal, frame):
    os.close(f)                 
    print 'Head file closed'
    sys.exit(0)

f = os.open('/dev/head', os.O_RDONLY)


signal.signal(signal.SIGINT, signal_handler)

list = []
while True:
    charout = os.read(f, 64)
    for i in range(IR_OFFSET, IR_OFFSET + 4):
         list.append("%02x" % (ord( charout[i]))),
    list.reverse()
    string = "".join(list)
    print (int(string,16)/256.0)*5.03
    string = ""
    list = []
    print "\n"
    time.sleep(1)

